package com.hkrt.demo.presentation.base

import android.app.ProgressDialog
import android.os.Bundle
import android.support.annotation.CallSuper
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import com.alibaba.android.arouter.launcher.ARouter
import com.hkrt.arch.BaseMVPActivity
import com.hkrt.demo.App
import com.hkrt.demo.R
import com.hkrt.demo.data.pref.Preferences
import com.hkrt.demo.di.component.ActivityComponent
import com.hkrt.demo.di.module.ActivityModule
import com.hkrt.demo.presentation.utils.S
import com.hkrt.demo.presentation.utils.extensions.emptyString
import com.hkrt.demo.presentation.utils.extensions.unSafeLazy
import com.hkrt.demo.presentation.widget.MaterialDialog
import com.hkrt.util.ToastUtils
import com.trello.rxlifecycle2.LifecycleTransformer
import javax.inject.Inject

abstract class BaseActivity<V : BaseContract.View, P : BaseContract.Presenter<V>>
    : BaseMVPActivity<V, P>() {

    @Inject
    lateinit var inflater: LayoutInflater

    @Inject
    lateinit var preferences: Preferences

    private var dialog: MaterialDialog? = null

    protected var progressDialog: ProgressDialog? = null

    override fun <T> getRxLifeCycle(): LifecycleTransformer<T> {
        return bindToLifecycle()
    }

    lateinit var activityComponent: ActivityComponent

    @CallSuper
    override fun onCreate(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        activityComponent = getAppComponent().plus(ActivityModule(this))
        injectDependencies()
//        navigator.fragmentChangeListener = this
        super.onCreate(savedInstanceState)
    }

    @CallSuper
    override fun onDestroy() {
        dialog?.dismiss()
        super.onDestroy()
    }

    @CallSuper
    override fun onBackPressed() {
//        if (navigator.hasBackStack())
//            navigator.goBack()
//        else
        super.onBackPressed()
    }

    protected abstract fun injectDependencies()

    private fun getAppComponent() = App.instance.applicationComponent

    inline protected fun <reified T : Fragment> goTo(keepState: Boolean = true,
                                                     withCustomAnimation: Boolean = false,
                                                     arg: Bundle = Bundle.EMPTY) {
//        navigator.goTo<T>(keepState = keepState,
//                withCustomAnimation = withCustomAnimation,
//                arg = arg)
    }

    fun showDialog(title: String, message: String, buttonText: String = "取消") {
        dialog = MaterialDialog(this).apply {
            message(message)
                    .title(title)
                    .addPositiveButton(buttonText) {
                        hide()
                    }
                    .show()
        }
    }

    fun showErrorDialog(message: String?, buttonText: String = "关闭") {
        showDialog(getString(S.error_title), message ?: emptyString, buttonText)
    }

    /**
     * 加载框，默认不能取消，用系统默认的文字提示
     */
    fun showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = ProgressDialog(this)
        }
        progressDialog!!.setCancelable(false)
        progressDialog!!.setMessage(resources.getString(R.string.show_progress))
        progressDialog!!.show()
    }

    /**
     * 加载框，自定义是否可以取消，用系统默认的文字提示
     */
    fun showProgressDialog(cancelable: Boolean) {
        if (progressDialog == null) {
            progressDialog = ProgressDialog(this)
        }
        progressDialog!!.setCancelable(cancelable)
        progressDialog!!.setMessage(resources.getString(R.string.show_progress))
        progressDialog!!.show()
    }

    /**
     * 加载框，自定义是否可以取消，自定义的文字提示
     */
    fun showProgressDialog(cancelable: Boolean, message: String) {
        if (progressDialog == null) {
            progressDialog = ProgressDialog(this)
        }
        progressDialog!!.setCancelable(cancelable)
        progressDialog!!.setMessage(message)
        progressDialog!!.show()
    }

    fun hideProgressDialog() {
        if (progressDialog != null) {
            progressDialog!!.dismiss()
        }
    }

    fun showLoading(text: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    fun showContent() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    fun showEmpty(text: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    fun showError(text: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    fun showErrorMsg(msg: String?) {
        ToastUtils.show(this, msg);
    }


}
